import Tittle from "../Tittle";
import AddDeptForm from "../AddDept";
import FundMen from "../FundMen";
import { Select, Option } from "@ui/RJMart/select";
import Icon  from "@ui/RJMart/icons";
import HttpUtils from "@page/common/utils/HttpUtils";
import UrlHooks from "../../../../../pages/ruijing/common/utils/urlHook";
import "./style.scss";
import ZenModal from "../../../../../components/zenUi/ZenModal/ZenModal";
import message from "@ui/RJMart/message";

class SubjectInfo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            DeptList: null,
            selectVal: [],
            isFrozen:0,
            frozenMessage:""
        };
    }

    componentDidMount() {
        this.getDeptsW();
    }
    getDeptsW=()=>  {
        HttpUtils.getDeptsW({})
            .then(res => {
                console.log(res);
                let deptsId =
                    res.depts && res.depts[0] ? res.depts[0].id.toString() : "";
                let testDepts = res.depts;
                console.log("getDeptsW",deptsId,res.depts[0]);
                this.getFrozen(deptsId);
                this.setState({
                    DeptList: testDepts,
                    selectVal: deptsId ? [deptsId] : []
                });
                this.props.changeDep(res.depts[0]);
                console.log("getDeptsW")
            })
            .catch(err => {
                console.log(err);
            });
    }

    //新增课题组之后重新获取所有课题组并选中新增的课题组
    getDeptsW2=(selectVal)=>  {
        HttpUtils.getDeptsW({})
            .then(res => {
                console.log(res);
                let deptsId =
                    res.depts && res.depts[0] ? res.depts[0].id.toString() : "";
                this.setState({
                    DeptList: res.depts,
                    selectVal: deptsId ? [deptsId] : []
                });
                console.log("getDeptsW");
                this.setState({
                    selectVal
                });
                let copyDeptList = this.state.DeptList;
                let result = copyDeptList.filter((v, i) => {
                    return selectVal[0] === v.id;
                });
                // 这里要注意：setState 是一个异步方法，所以需要操作缓存的当前值
                this.props.changeDep(result[0]);
                console.log("changeDep",selectVal);
            })
            .catch(err => {
                console.log(err);
            });
    }

    showModal=(SubjectInfo)=>  {

        console.log("showModal",SubjectInfo);
        function closeModal(value) {
            console.log("run closeModal", value);
            modal.hide();
        }

        function Content(props) {
            props.item.passParams && props.item.passParams("abc");
            props.item.changeDep = SubjectInfo.changeDep;
            props.item.getDeptsW = SubjectInfo.getDeptsW2;
            console.log("props",props);
            return AddDeptForm(props);
        }
        let options = {
            header: "添加课题组",
            // footer: "test footer",
            content: Content,
            size: "md",
            //cbList: [AddDeptForm.doSubmit],
            close: closeModal,
            hideFooter:true
        };
        let modal = ZenModal.loadModal(options);
        modal.show();
    }

    changeDep=(selectVal)=> {
        this.setState({
            selectVal
        });
        let copyDeptList = this.state.DeptList;
        let result = copyDeptList.filter((v, i) => {
            return selectVal[0] === v.id;
        });
        // 这里要注意：setState 是一个异步方法，所以需要操作缓存的当前值
        this.getFrozen(result[0].id);
        this.props.changeDep(result[0]);
        this.props.selectMen([]);
        console.log("changeDep",selectVal);
    };

    toPurchaseCenter=()=>{
        let targetUrl = this.state.isFrozen==1?UrlHooks.order6:UrlHooks.daijiesuan2;
        console.log("toPurchaseCenter",this.state.isFrozen,targetUrl);
        if(this.state.isFrozen!=0) {
            window.location.href = targetUrl;
        }
    }

    //获取冻结信息
    getFrozen=(deptId)=>{
        const orgId = Uts.getCookie("orgId");
        console.log("getFrozen",deptId)
        if(deptId!="") {
            HttpUtils.getFrozenInfo(deptId)
                .then(res => {
                    console.log(res);
                    //this.setState({isFrozen: 1,frozenMessage:"该课题组有3张订单超过7天未验收，请验收后进行采购。"});
                    //this.setState({isFrozen: 2,frozenMessage:"该课题组有3张订单超过7天未结算，请结算后进行采购。"});
                    //this.props.updateFrozen(1);
                    if (res.code == "0000") {
                        this.setState({isFrozen: 0,frozenMessage:""});
                        this.props.updateFrozen(0);
                    } else if (res.code == "0004" && orgId != 3) {
                        this.setState({isFrozen: 1,frozenMessage:res.message});
                        this.props.updateFrozen(1);
                    } else if (res.code == "0005" && orgId != 3) {
                        this.setState({isFrozen: 2,frozenMessage:res.message});
                        this.props.updateFrozen(2);
                    } else {
                        message.warn({text: r.message || "", duration: 3000});
                    }
                })
                .catch(err => {
                    message.warn({text: err || "", duration: 3000});
                });
        }
    }

    render() {
        let SubjectInfo = this;
        console.log("SubjectInfo",SubjectInfo);
        const { DeptList, selectVal } = this.state;
        console.log("render",this.state);
        return (
            <div className="rj_subject_info">
                <div className="rj_subject-wp">
                    <Tittle text="课题项目信息" />
                    <div className="rj_chose-sub">
                        {DeptList&&(DeptList.length>1||(DeptList.length==1&&DeptList[0].name!="暨南大学初始部门"))&&(
                        <span className="rj_chose-sub-title">选择课题组</span>
                        )}
                        {DeptList&&DeptList.length==1&&DeptList[0].name=="暨南大学初始部门"&&(
                            <div className="rj_chose-sub-title zen_w-p100">选择课题组</div>
                        )}
                        {DeptList&&(DeptList.length>1||(DeptList.length==1&&DeptList[0].name!="暨南大学初始部门"))&&(
                        <Select
                            selection={selectVal}
                            onChange={this.changeDep}
                            placeholder="课题组名称"
                            className="zen_v-m"
                        >
                            {DeptList &&
                                DeptList.map((val, index) => {
                                    return (
                                        <Option key={index} value={val.id}>
                                            {val.name}
                                        </Option>
                                    );
                                })}
                        </Select>
                        )}
                        {DeptList&&(DeptList.length>1||(DeptList.length==1&&DeptList[0].name!="暨南大学初始部门"))&&(
                        <span
                            className={["37", "38"].indexOf(Uts.getCookie("orgId")) > -1 ? "rj_l-addDept-btn zen_m-40 zen_cur-p zen_0093EB zen_fs-14" : "zen_hide"}
                            onClick={() => this.showModal(SubjectInfo)}>
                            添加课题组
                        </span>
                        )}
                        {DeptList&&DeptList.length==1&&DeptList[0].name=="暨南大学初始部门"&&(
                            <div className="zen_w-p100">
                                <Icon type={"exclamation-circle-fill"}
                                      color={"#0093EB"}
                                      size={"small"}
                                      width={20}
                                      hzen_m-t-10eight={20}
                                      className="zen_m-10 zen_v-m"
                                />
                                <div className="zen_v-m zen_il-bl">
                                你还没有加入课题组，请先
                                <span
                                    className={["37", "38"].indexOf(Uts.getCookie("orgId")) > -1 ? "rj_l-addDept-btn zen_cur-p zen_0093EB zen_fs-12" : "zen_hide"}
                                    onClick={() => this.showModal(SubjectInfo)}>加入课题组</span>
                                </div>
                            </div>
                        )}
                        {this.state.isFrozen==1&&(
                            <div className="zen_red zen_m-t-10" style={{marginLeft:"120px"}}>{this.state.frozenMessage}<div className="zen_0093EB zen_cur-p zen_il-bl zen_m-l-10" onClick={this.toPurchaseCenter}>去验收></div></div>
                        )}
                        {this.state.isFrozen==2&&(
                            <div className="zen_red zen_m-t-10" style={{marginLeft:"120px"}}>{this.state.frozenMessage}<div className="zen_0093EB zen_cur-p zen_il-bl zen_m-l-10" onClick={this.toPurchaseCenter}>去结算></div></div>
                        )}

                    </div>
                    {this.state.selectVal.length>0&&Uts.getCookie("orgId")==38&&(
                        <FundMen
                            deptId={this.state.selectVal[0]}
                            selectMen ={this.props.selectMen}
                        />
                    )}
                </div>
            </div>
        );
    }
}

export default SubjectInfo;
